﻿#include "iddialog.h"
#include "ui_iddialog.h"
#include<QString>
#include<QDebug>
#include<QMessageBox>
#include<QDebug>
#include"sqlconnect.h"
IDDialog::IDDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::IDDialog)
{
    ui->setupUi(this);
    ui->nameEdit->setText("");
    ui->IDEdit->setText("");
    connect(ui->cancelButton,SIGNAL(clicked(bool)),this,SLOT(close()));
    connect(ui->confirmButton,SIGNAL(clicked(bool)),this,SLOT(react()));
    //connect(ui->confirmButton,SIGNAL(clicked(bool)),this,SLOT(close()));
}
IDDialog::~IDDialog()
{
    delete ui;
}
void IDDialog::react(){
    //先判断证件类型、后插入姓名、证件类型、证件号
    QString name=ui->nameEdit->text();
    QString credtype=ui->idBox->currentText();
    QString ID=ui->IDEdit->text();
    //判断证件类型
    QString credtypeid;
    QSqlQuery query("SELECT CredTypeID FROM CredTypeCode where CredType='"+credtype+"'");
    while (query.next()) {
    credtypeid = query.value(0).toString();
    }
    if(name.count()==0||ID.count()==0){
        QMessageBox::information(this,"提示信息","输入不符合格式！");
        return;
        }
    //qDebug()<<credtype<<"leixing"<<credtypeid;
    QSqlQuery query1;
    QString sql1="SELECT CadreName FROM `ZZB`.`CadreInfo` where IDNum='"+ID+"'";
    bool ok1=query1.exec(sql1);
    query1.next();
    QString nameGeted=query1.value(0).toString();
    //qDebug()<<ok1<<"   "<<nameGeted;
    //如果名字为空，则可插入
    if(nameGeted.count()==0){

    QString sql2="INSERT INTO CadreInfo(CadreName,CredTypeID,IDNum)VALUES('"+name+"','"+credtypeid+"','"+ID+"')";
    qDebug()<<sql2;
    bool ok2=query.exec(sql2);
    if(ok2){

            qDebug()<<"成功插入干部信息";

      }
    else{
            qDebug()<<"证件号插入失败";
      }
        emit sendData(ui->nameEdit->text(),ui->IDEdit->text());
    }
    else {
            QMessageBox::information(this,"提示信息","此人已导入任免表!");
            return;
        }
    this->close();
}
